import urllib.request
import urllib.parse
import json

#post请求

url = 'https://fanyi.baidu.com/sug'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
}

data = {
    'kw': 'spider'
}

#post请求的参数 必须要进行编码
data = urllib.parse.urlencode(data).encode('utf-8')

request = urllib.request.Request(url=url, data=data, headers=headers)

# 模拟浏览器向服务器放松请求
response = urllib.request.urlopen(request)

#获取响应的数据
# print(response)
content = response.read().decode('utf-8')
# print(content)
# 字符串--》 json对象
obj = json.loads(content)
print(obj)
